﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="Ornament.MemberShip" %>
<%@ Import Namespace="Ornament.Messages" %>
<%@ Import Namespace="Ornament.Web" %>
<%@ Import Namespace="Ornament.MemberShip.Permissions" %>
<link href="/Scripts/lib/treeView/jquery.treeview.css" rel="stylesheet" type="text/css" />
<script type="text/C#" runat="server">
    ////Key is the InfoType's Id, Value is the Resource's Id
    //Dictionary<Guid, Guid> listId = new Dictionary<Guid, Guid>();
    //private void buldListId()
    //{
    //    var infosTypeRes = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateResourceDao().
    //        GetResourceByType(typeof(GenericResourceInfo<InfoType>));
    //    foreach (GenericResourceInfo<InfoType> res in infosTypeRes)
    //    {
    //        listId.Add(res.ResourceKey.Id, res.Id);
    //    }
    //}
    //private Guid BuildResource(InfoType type, Ornament.MemberShip.IResourceDao dao)
    //{
    //    if (listId.Count == 0)
    //    {
    //        buldListId();
    //    }

    //    foreach (var id in listId.Keys)
    //    {
    //        if (type.Id == id)
    //            return listId[id];
    //    }
    //    var res = new GenericResourceInfo<InfoType>(type) { Name = type.Name };
    //    dao.Save(res);
    //    return res.Id;
    //}

    private void Build(IEnumerable<InfoType> childNodes)
    {
        var dao = OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateResourceDao();

        foreach (var node in childNodes)
        {
            //var resId = BuildResource(node, dao);
            if (node.Count == 0)
            {
                Response.Write(String.Format(@"<li><input type=""radio"" value=""{0}""/><label>{1}<label></li>", resId.ToString(), node.Name, node.Remark));
            }
            else
            {
                Response.Write(String.Format(("<li><a id='{0}' title='{2}'>{1}</a>"), resId.ToString(), node.Name, node.Remark));
                Response.Write("<ul>");
                this.Build(node.GetChilds());
                Response.Write("</ul>");
                Response.Write("</li>");
            }
        }
    }

    IList<InfoType> infoTypes = OrnamentContext.Current.GetDaoFactory<InfoManager>().Factory.InfoTypeDao.GetFirstLevel();
</script>
<script src="/Scripts/Lib/treeView/jquery.treeview.js" type="text/javascript"></script>
<input type="hidden" name="resourceId" id="resourceId" />
<input type="hidden" name="resourceType" id="resourceType" />
<div id="infoTypeSelectorPanel">
    <ul id="infoTypeTree">
        <li><a href="#<%=Guid.Empty.ToString()%>">无父类型</a>
            <ul>
                <%
                    Build(infoTypes);
                %>
            </ul>
        </li>
    </ul>
</div>
<script type="text/javascript">
    $("a", $("#infoTypeTree").treeview()).click(function () {
        var code = $("#resourceId").val();
        if (code != "") {
            $("#" + code).removeClass("ui-state-default");
        }
        var id = $(this).addClass("ui-state-default").attr("id");
        
        $("#ResourceId").val(id);
    });
</script>
